//
//  Presentation.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/7/18.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON



protocol HealTabViewDelegate: class {
    func selectBtn(senderId: Int)
}
/// 组件
class HealTabView : UIView {
    let btn1 = UIButton()
    let btn2 = UIButton()
    let btn3 = UIButton()
    weak var delegate: HealTabViewDelegate?
  
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        self.addSubview(btn1)
        self.addSubview(btn2)
        self.addSubview(btn3)
        
        
        

//        btn1
        
        btn2.setTitle("健康报告", for: UIControlState.normal)
        btn2.setTitleColor(UIColor.white, for: .selected)
        btn2.setTitleColor(UIColor.black, for: .normal)
        btn2.centerYAlign(view: self)
        btn2.centerXAlign(view: self)
        btn2.width(view: self, predicate: "*0.25")
        btn2.isSelected = false
        btn2.addTarget(self, action: #selector(selectClick), for: UIControlEvents.touchUpInside)
        btn2.tag = 1
        
        btn1.setTitle("全部", for: UIControlState.normal)
        btn1.isSelected = true
        btn1.setTitleColor(UIColor.white, for: .selected)
        btn1.setTitleColor(UIColor.black, for: .normal)
        btn1.centerYAlign(view: self)
        btn1.leadingAlign(view: self)
        btn1.width(view: self, predicate: "*0.25")
        btn1.layer.borderWidth = 1
        btn1.layer.borderColor = configure.shareConfigure.HMColor1.cgColor
        btn1.backgroundColor = configure.shareConfigure.HMColor1
        btn1.layer.cornerRadius = 15
        btn1.addTarget(self, action: #selector(selectClick), for: UIControlEvents.touchUpInside)
        btn1.tag = 0
        
        
        btn3.setTitle("专项报告", for: UIControlState.normal)
        btn3.setTitleColor(UIColor.white, for: .selected)
        btn3.setTitleColor(UIColor.black, for: .normal)
        btn3.isSelected = false
        btn3.centerYAlign(view: self)
        btn3.trailingAlign(view: self)
        btn3.width(view: self, predicate: "*0.25")
        btn3.addTarget(self, action: #selector(selectClick), for: UIControlEvents.touchUpInside)
        btn3.tag = 2
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func selectClick(sender: UIButton) {
       // sender.isSelected = !sender.isSelected
        
        if sender.tag == 0  {
            self.btn1.isSelected = true
            self.btn2.isSelected = false
            self.btn3.isSelected = false
            
            self.btn2.backgroundColor = UIColor.white
            self.btn2.layer.borderColor = UIColor.white.cgColor
            
            self.btn3.backgroundColor = UIColor.white
            self.btn3.layer.borderColor = UIColor.white.cgColor
        }else if sender.tag == 1 {
            self.btn1.isSelected = false
            self.btn2.isSelected = true
            self.btn3.isSelected = false
            
            self.btn1.backgroundColor = UIColor.white
            self.btn1.layer.borderColor = UIColor.white.cgColor
            
            self.btn3.backgroundColor = UIColor.white
            self.btn3.layer.borderColor = UIColor.white.cgColor
            
        }else {
            self.btn1.isSelected = false
            self.btn2.isSelected = false
            self.btn3.isSelected = true
            
            self.btn1.backgroundColor = UIColor.white
            self.btn1.layer.borderColor = UIColor.white.cgColor
            
            self.btn2.backgroundColor = UIColor.white
            self.btn2.layer.borderColor = UIColor.white.cgColor
        }
        sender.backgroundColor = configure.shareConfigure.HMColor1
        sender.layer.borderWidth = 1
        sender.layer.borderColor = configure.shareConfigure.HMColor1.cgColor
        sender.layer.cornerRadius = 15
        
        delegate?.selectBtn(senderId: sender.tag)
        
    }
    
    
    
 
    
}


//  体检报告
class Presentation: HMViewController {
    
    
    // MARK: - variable
    var jsonData: JSON = JSON()
    var p_type: Int = 0  // 全部= 0 ， 健康 = 1 ， 专项 = 2
    var reportListDic = Dictionary<String, Any>() //请求的参数集合
    
    
    let healthyView = UIView()
    let specialView = UIView()
    let healthy = UIButton()
    let special = UIButton()
    
    
    let head = UIView()
    let headTitle = UILabel()
    let headColorView = UIView()
    let healTabView = HealTabView()
    let p_table = UITableView()
    // MARK: - 配置view
    override func setupView() {
        self.title = "体检报告"
        view.backgroundColor = UIColor.RGBA(r: 239, g: 239, b: 239, a: 1)
        view.addSubview(p_table)
        
        view.addSubview(healthyView)
        view.addSubview(specialView)
        healthyView.addSubview(healthy)
        specialView.addSubview(special)
        
        
        head.addSubview(headColorView)
        head.addSubview(headTitle)
        view.addSubview(head)
        view.addSubview(healTabView)
        
        // 两个按钮
        healthyView.leadingAlign(view: view, predicate: "0")
        healthyView.topAlign(view: view, predicate: "2")
        healthyView.width(view: view, predicate: "*0.5")
        healthyView.heightConstrain(predicate: "120")
        healthyView.backgroundColor = UIColor.white
        
        specialView.trailingAlign(view: view, predicate: "2")
        specialView.topAlign(view: view, predicate: "2")
        specialView.width(view: view, predicate: "*0.5")
        specialView.heightConstrain(predicate: "120")
        specialView.backgroundColor = UIColor.white
        
    
        healthy.centerXYAlign(view: healthyView)
        healthy.heightConstrain(predicate: "120")
        healthy.setImage(UIImage.init(named: "healthy.png"), for: UIControlState.normal)
        healthy.addTarget(self, action: #selector(healthyClick(_:)), for: .touchUpInside)
        
        special.centerXYAlign(view: specialView)
        special.heightConstrain(predicate: "120")
        special.setImage(UIImage.init(named: "special.png"), for: UIControlState.normal)
        special.addTarget(self, action: #selector(specialClick), for: .touchUpInside)

        
        // 体检报告
        head.topVertically(view: healthyView)
        head.leadingAlign(view: view, predicate: "0")
        head.trailingAlign(view: view, predicate: "0")
        head.backgroundColor = UIColor.white
        head.constrainHeight("44")

        headColorView.centerYAlign(view: head)
        headColorView.leadingAlign(view: head)
        headColorView.backgroundColor = configure.shareConfigure.HMColor1
        headColorView.constrainWidth("10")
        headColorView.constrainHeight("30")

        headTitle.text = "我的体检报告"
        headTitle.centerYAlign(view: head)
        headTitle.leadingHorizontally(view: headColorView, predicate: "10")
        
        
        
        // 选项卡
        healTabView.topVertically(view: head, predicate: "10")
        healTabView.delegate = self
        healTabView.leadingAlign(view: view, predicate: "0")
        healTabView.trailingAlign(view: view, predicate: "0")
        healTabView.heightConstrain(predicate: "44")
        
        p_table.topVertically(view: healTabView, predicate: "10")
        p_table.leadingAlign(view: view, predicate: "0")
        p_table.trailingAlign(view: view, predicate: "0")
        p_table.bottomAlign(view: view)
        p_table.delegate = self
        p_table.dataSource = self
        p_table.register(HMPresentationCell.classForCoder(), forCellReuseIdentifier: "HMPresentationCell")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        print("更新一次？")
        self.upadeTableData()
        
    }
    
    @objc func specialClick(_ sender: UIButton) {
        
        NetRequestTool.shareInstance.met = .get
        NetRequestTool.request(url: "user/exam-report/config", parameters: [:], success: { (json) in
            
            let _special = SpecialPresentation()
            _special.spJsonData = json["data"]
            self.pushViewController(_special)
            
        }, fail: { (code, msg) in
            print("Presentation---->", msg)
        }) {}
        

     }
    
    @objc func healthyClick(_ sender: UIButton) {
            let _entry = HMEntryReport() // 健康报告
            _entry.erType = 1
            _entry.isEditState = false // 不是编辑状态
            self.pushViewController(_entry) 
        
    }
    
}

extension Presentation : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.jsonData.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = p_table.dequeueReusableCell(withIdentifier: "HMPresentationCell") as! HMPresentationCell
            cell.setJson(json: jsonData[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let entry = HMEntryReport()
        entry.erJsonData =  self.jsonData[indexPath.row]
        
        let ary = self.jsonData[indexPath.row]["images"].arrayObject
        entry.photoUrls =  NSMutableArray.init(array: ary!)
        entry.isEditState = true
        self.pushViewController(entry)
    }
    
    
}


extension Presentation: HealTabViewDelegate {
    func selectBtn(senderId: Int) {
        if senderId == 0 {
            reportListDic.removeValue(forKey: "type")
        }else if senderId == 1 {
            reportListDic["type"] = senderId
//        }else {
//            reportListDic["type"] = 2
//
        }
        upadeTableData()
    }
    
    // 更新数据
    func upadeTableData() {
        NetRequestTool.shareInstance.met = .get
        NetRequestTool.request(url: "user/exam-report/list", parameters: reportListDic, success: { (json) in
            self.jsonData = json["data"]
            self.p_table.reloadData()
        }, fail: { (code, msg) in
            print("----->>", msg)
        }) { }
    }
}
